package clids.ex2.filescript;

import java.util.Scanner; 
import java.util.LinkedList;

import clids.ex2.actions.ActionFactory;
import clids.ex2.filters.DefaultFilter;
import clids.ex2.filters.FilterFactory;
import clids.ex2.filters.AbstractFilter;
import clids.ex2.filters.NotFilter;
import clids.ex2.orders.OrderFactory;

public class CommandFileParser {

	private static final String NOT_FILTER = "NOT";
	private static final int ORDER_STATE = 3;
	private static final int ACTION_STATE = 2;
	private static final int FILTER_STATE = 1;
	private Scanner scanner;

	public CommandFileParser(String filePath) {
		scanner = new Scanner(filePath);
		if (scanner == null) {
			// TODO Auto-generated catch block
			System.out.println("prbolem with scanner");
		}
	}

	public LinkedList<Section> Parse(){
		LinkedList<Section>  list = new LinkedList<>();
		
		Section section=null;
		String line=null;
		boolean subSectionStisfaction = false;
		int state = 0;
		
		//State machine parsing
		try{
	
			while(scanner.hasNext()){
				
				line=scanner.nextLine();
				
				if(line.equals("FILTER")){
					if(section != null){
						//check section validity
						// TODO Auto-generated catch block
						list.addLast(section);
					}
					section = new Section();
					state = FILTER_STATE;
					subSectionStisfaction=false;
					//put default filter (all) - may be replaced later
					section.setFilter(FilterFactory.GetFilter("default"));
					continue;
				}
				if(line.equals("ACTION")){
					
					state = ACTION_STATE;
					subSectionStisfaction=false;
					continue;
				}
				if(line.equals("ORDER"))
				{
					state = ORDER_STATE;
					subSectionStisfaction=false;
					//Setting default Order - it may be replaced later
					section.setOrder(OrderFactory.GetOrder("abs"));
					continue;
				}
				if(line.startsWith("@")){
					if(state == 0){
						//throw an exception
					}
					section.addComment(line.substring(1));
					continue;
				}

				if(state == FILTER_STATE){
					//handle the not keyword
					if(subSectionStisfaction == true){
						//TODO Throw an exception
					}
					String[] splittedLine = line.split("%",1);
					AbstractFilter filter=null;
					filter = FilterFactory.GetFilter(splittedLine[0],splittedLine[1]);
					
					if(line.endsWith(NOT_FILTER)){
						filter = new NotFilter(filter);
					}

					section.setFilter(filter);
					subSectionStisfaction=true;
					continue;
				}
				if(state == ACTION_STATE){
					//Action subsection might be empty.
					String[] splittedLine = line.split("%",1);
					AbstractAction action = ActionFactory.GetAction(splittedLine[0]);
					if(splittedLine.length > 1){
						action.setParameters(splittedLine[1]);
					}
					section.addAction(action);
					continue;
				}
				if( state == ORDER_STATE){
					if(subSectionStisfaction == true){
						//TODO Throw an exception
					}
					String[] splittedLine = line.split("%",1);
					section.setOrder(OrderFactory.GetOrder(splittedLine[0]));
					subSectionStisfaction=true;		
					continue;
				}
				// TODO Auto-generated catch block
				System.out.println("not all file parsed");
				

				String[] splittedLine = line.split("%",1);
				AbstractFilter filter = FilterFactory.GetFilter(splittedLine[0]);
				//Setting the parSameters
				filter.setParameters(splittedLine[1]);
				//Saving the Filter

			}
		}
		catch(NullPointerException e){
			// TODO Auto-generated catch block
		}

		
		return list;
		
	}
}
